//============== Wii-Controller (impl. WiiInterface) =================
#include "WiiControlling.h"
//------------------------------------------------------------------------
//const float ControlInterface::MAX_MOVEMENT_DISTANCE = 50.0f;
namespace WiiMote {
//------------------------------------------------------------------------
// Kon-/Destruktoren
//------------------------------------------------------------------------
WiiControlling::WiiControlling()
{
	printf("MAX... %f\n", MAX_MOVEMENT_DISTANCE);
	this->max_x = 560;
	this->max_y = 420;
	this->max_delta = 0.06f;
	this->max_wiimotes = 1;
	int found, connected;

	this->wiimotes = wiiuse_init(max_wiimotes);   //Feld initialisieren

    found = wiiuse_find(wiimotes, max_wiimotes, 5); //Wiimotes suchen (muessen dafuer schon verbunden sein!)
    if (!found) {
        printf ("Keine Wiimotes gefunden.\n");
    }

	connected = wiiuse_connect(wiimotes, max_wiimotes); //Wiimotes verbinden (mit der Anwendung)
	if (connected)
		printf("Verbunden zu %i Wiimotes (von %i gefundenen).\n", connected, found);
	else {
		printf("Konnte keine Verbindung zu einer Wiimote herstellen.\n");
	}

	wiiuse_set_ir(wiimotes[0], 1);

    wiiuse_rumble(wiimotes[0], 1); //Hallo sagen
    Sleep(200);
	wiiuse_rumble(wiimotes[0], 0);

	//this->thread = CreateThread(nullptr, 0, threadFunction, nullptr, 0, nullptr);
}

WiiControlling::~WiiControlling()
{
	wiiuse_cleanup(this->wiimotes, this->max_wiimotes);
}
//------------------------------------------------------------------------
// Methoden
//------------------------------------------------------------------------
bool WiiControlling::pitchCameraUp() const
{
	wiiuse_poll(wiimotes, max_wiimotes);		// von philipph: wieso hier poll aber bei allen anderen methoden nicht? oO
	return IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_UP);
}

bool WiiControlling::pitchCameraDown() const
{
	return IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_DOWN);
}

bool WiiControlling::yawCameraRight() const
{
	return IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_RIGHT);
}

bool WiiControlling::yawCameraLeft() const
{
	return IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_LEFT);
}

vector2df const& WiiControlling::moveCursor() const
{
	static vector2df pos;
	static vector2df delta;
	delta.X = (wiimotes[0]->ir.x - pos.X) / MAX_MOVEMENT_DISTANCE;
	delta.Y = (wiimotes[0]->ir.y - pos.Y) / MAX_MOVEMENT_DISTANCE;
	pos.X = (float)(wiimotes[0]->ir.x);
	pos.Y = (float)(wiimotes[0]->ir.y);
	if(pos.X == max_x) delta.X = max_delta;
	if(pos.Y == max_y) delta.Y = max_delta;
	if(pos.X == 0) delta.X = -max_delta;
	if(pos.Y == 0) delta.Y = -max_delta;
	//printf("deltaX: %.3f, deltaY: %.3f\n", pos.X, pos.Y);
	return delta;
}

bool WiiControlling::rollObjectRight() const
{
	return false;
}

bool WiiControlling::rollObjectLeft() const
{
	return false;
}

bool WiiControlling::pitchObjectFront() const
{
	return false;
}

bool WiiControlling::pitchObjectBack() const
{
	return false;
}

bool WiiControlling::yawObjectLeft() const
{
	return false;
}

bool WiiControlling::yawObjectRight() const
{
	return false;
}

static DWORD WINAPI threadFunction(LPVOID lpParam){
	
	
	return 0;
}

}; // Namespace WiiMote

